//
//  WBEmotionListView.m
//  新浪微博项目
//
//  Created by neng on 15/12/21.
//  Copyright (c) 2015年 neng. All rights reserved.
//


#define WBNumOfEmotionsInOnePage 20

#import "WBEmotionListView.h"
#import "WBEmotionPageView.h"

@interface WBEmotionListView () <UIScrollViewDelegate>

@property (nonatomic,weak) UIPageControl *pageControll;
@property (nonatomic,weak) UIScrollView *scrollView;

@end

@implementation WBEmotionListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //1.scrollView
        UIScrollView *scrollView = [[UIScrollView alloc] init];
    
        //这样可以去掉scrollView内部的两个控件
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        //2.pageControll
        UIPageControl *pageControll = [[UIPageControl alloc] init];
        pageControll.userInteractionEnabled = NO;
        
        //设置原点的图片
        [pageControll setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKeyPath:@"currentPageImage"];
        [pageControll setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKeyPath:@"pageImage"];
        
        [self addSubview:pageControll];
        self.pageControll = pageControll;
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //1.pageControll
    self.pageControll.width = self.width;
    self.pageControll.height = 35;
    self.pageControll.x = 0;
    self.pageControll.y = self.height - self.pageControll.height;
 
    //2.scrollView
    self.scrollView.width = self.width;
    self.scrollView.height = self.pageControll.y;
    self.scrollView.x = 0;
    self.scrollView.y = 0;
    
    //3.scrollView里的表情页
    NSInteger count = self.scrollView.subviews.count;
    CGFloat pageW = self.scrollView.width;
    CGFloat pageH = self.scrollView.height;
    CGFloat pageY = 0;
    for (int i = 0; i < count; i++) {
        WBEmotionPageView *emotionPage = self.scrollView.subviews[i];
        CGFloat pageX = i * self.scrollView.width;
        emotionPage.frame = CGRectMake(pageX, pageY, pageW, pageH);
    }
    
    //好像contentSize只能在这修改
    self.scrollView.contentSize = CGSizeMake(count * pageW, 0 );
    
}

- (void)setEmotions:(NSArray *)emotions
{
    //先把scrollView上面的子控件移除，不然之前那一页不会移除而再创建一个新的页
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger page = (emotions.count + WBNumOfEmotionsInOnePage - 1) / WBNumOfEmotionsInOnePage ;
    
    self.pageControll.numberOfPages = page;
    
    //根据页数创建N个用来承接表情的view
    for (int i = 0 ; i < page; i++) {
        WBEmotionPageView *emotionPage = [[WBEmotionPageView alloc] init];
        
        //判断剩下的有多少个，少于20个就只截取剩下的
        NSInteger loc = i *WBNumOfEmotionsInOnePage;
        NSInteger left = emotions.count - loc ;
        NSArray *arr = nil;
        if (left > WBNumOfEmotionsInOnePage) {
            arr = [emotions subarrayWithRange:NSMakeRange(loc, WBNumOfEmotionsInOnePage)];
        }else {
            arr = [emotions subarrayWithRange:NSMakeRange(loc, left)];
        }
        
        emotionPage.emotions = arr;
        
        [self.scrollView addSubview:emotionPage];
    }
    
    //要重新布局，不然就看不到新的一页
    [self setNeedsLayout];
}

//设置当前页
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x / scrollView.width + 0.5;
    self.pageControll.currentPage = page;

}

@end
